package edu.lingnan.house.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.lingnan.house.pojo.TbCollection;
import edu.lingnan.house.pojo.TbUser;
import edu.lingnan.house.service.TbCollectionService;
import edu.lingnan.house.service.TbUserService;
import edu.lingnan.house.service.UserService;
import edu.lingnan.house.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/tbuser")
public class UserController {
    @Autowired (required = false)
    private TbUserService tbuserService;

    @Autowired (required = false)
    private UserService userService;

    @Autowired (required = false)
    private TbCollectionService tbCollectionService;

    @PostMapping("/update")
    @ResponseBody
    public TbUser updateById(HttpSession session){
        TbUser tbUser = tbuserService.getById(134);
        session.setAttribute("tbUser",tbUser);
        TbUser tbUser1 = (TbUser) session.getAttribute("tbUser");
        String id = tbUser1.getId();
        System.out.println("jjj"+tbUser1);
        Boolean row = tbuserService.updateById(tbUser1);

        if (!row){
            return null;
        }else {
            return tbuserService.getById(tbUser1);
        }
    }

    @PostMapping("/upUInfo")
    public Result<Void> upUInfo(String id, String name, String headPortrait, String password, String UPpassword, String TUPpassword){
        return userService.updateUInfo(id, name, headPortrait, password,UPpassword, TUPpassword);
    }

    //查看我的房源关注
    @GetMapping("/myCollection")
    @ResponseBody
    public Result myCollection(HttpSession session){
        TbUser tbUser = tbuserService.getById(134);
        session.setAttribute("tbUser",tbUser);


        //先写好，合并登陆注册在开
        TbUser tbUser1 = (TbUser) session.getAttribute("tbUser");

        if(tbUser1==null){
            throw  new RuntimeException("请先登录");
        }
        //通过userId查询关注信息
        String userId = tbUser1.getId();
        QueryWrapper<TbCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<TbCollection> tbCollections = tbCollectionService.list(queryWrapper);
        return new Result<>(200,tbCollections,"ok");
    }


}
